package tr.helper.domain;

import io.vavr.Tuple;
import io.vavr.Tuple2;
import java.util.function.Consumer;
import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;
import tr.helper.view.ServerInfoItem;

/**
 * tr-service
 *
 * @author MrYe
 * @date 2024-07-25 20:05
 */
public class ServerOperatorCell<S> extends TableCell<S, Void> {

    private final Button deployBtn;
    private final Button stopBtn;
    private final Button startBtn;
    private final Button rollbackBtn;
    private final Button taskBtn;

    public ServerOperatorCell(Consumer<Tuple2<Button, S>>... consumer) {
        deployBtn = new Button("部署");
        deployBtn.setOnAction(event -> {
            Tuple2<Button, S> params = Tuple.of(deployBtn, getTableView().getItems().get(getIndex()));
            consumer[0].accept(params);
        });
        stopBtn = new Button("停止");
        stopBtn.setDisable(true);
        stopBtn.setOnAction(event -> {
            Tuple2<Button, S> params = Tuple.of(stopBtn, getTableView().getItems().get(getIndex()));
            consumer[1].accept(params);
        });
        startBtn = new Button("启动");
        startBtn.setDisable(true);
        startBtn.setOnAction(event -> {
            Tuple2<Button, S> params = Tuple.of(startBtn, getTableView().getItems().get(getIndex()));
            consumer[2].accept(params);
        });
        rollbackBtn = new Button("回滚");
        rollbackBtn.setOnAction(event -> {
            Tuple2<Button, S> params = Tuple.of(rollbackBtn, getTableView().getItems().get(getIndex()));
            consumer[3].accept(params);
        });
        taskBtn = new Button("任务");
        taskBtn.setOnAction(event -> {
            Tuple2<Button, S> params = Tuple.of(rollbackBtn, getTableView().getItems().get(getIndex()));
            consumer[4].accept(params);
        });
    }

    @Override
    protected void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            setGraphic(createButtonsPane());
            if (getTableView().getItems().size() > 0) {
                ServerInfoItem a = (ServerInfoItem) getTableView().getItems().get(getIndex());
                Platform.runLater(() -> {
                    // 未运行
                    if (StringUtils.contains(a.getStat(), "0")) {
                        startBtn.setDisable(false);
                    }
                    if (StringUtils.contains(a.getStat(), "1")) {
                        startBtn.setDisable(true);
                    }
                    if (StringUtils.contains(a.getStat(), "2")) {
                        startBtn.setDisable(true);
                        stopBtn.setDisable(false);
                    }
                });
            }
        }
    }

    private VBox createButtonsPane() {
        HBox buttonsPane = new HBox(5);
        buttonsPane.getChildren().addAll(deployBtn, stopBtn, startBtn, rollbackBtn, taskBtn);
        VBox.setVgrow(buttonsPane, Priority.ALWAYS);
        return new VBox(buttonsPane);
    }

}
